<template>
	<view class="Box">
		<SwiperView :SwiperList="SwiperList"></SwiperView>
		<view class="bottom">
			<h4>&verbar;&ensp;历史推文</h4>
		</view>
		<IntroducePaper :item="IntroducePaperList"></IntroducePaper>
	</view>
</template>

<script>
	import '@/common/css/index.less'
	import API from '../../api/api.js'
	import SwiperView from './component/Swiper-View.vue'
	import IntroducePaper from './component/Introduce-Paper.vue'
	import IntroduceItem from '../../components/common/Introduce-Item.vue'
	export default {
		components: {
			SwiperView,
			IntroducePaper,
			IntroduceItem
		},
		data() {
			return {
				IntroducePaperList: [],
				SwiperList: [],
				currentPage: 1, // 当前页码
				PageSize: 10, // 每页显示10条
				loading: false, // 是否正在加载
				hasMore: true // 是否还有更多数据
			}
		},
		onLoad() {

		},
		methods: {
			async getSwiperImg(currentPage = 1, PageSize = 10) {
				try {
					const res = await API.getSwiperImg(currentPage, PageSize);
					if (res && res.data && res.data.records) {
						const records = res.data.records;
						records.forEach(record => {
							if (record.state && record.cover === 2) {
								this.SwiperList.push(record.url);
							}
						});
						// console.log("this.SwiperList", this.SwiperList);
					} else {
						console.error("No records found");
					}
				} catch (error) {
					console.error("Error fetching swiper images:", error);
				}
			},
			async getTweetsInfo(currentPage = 1, PageSize = 10) {
				if (this.loading || !this.hasMore) return; // 如果正在加载或者没有更多数据就不再请求
				this.loading = true;
				try {
					const {
						data
					} = await API.getTweets(currentPage, PageSize);
					if (data.records && data.records.length > 0) {
						this.IntroducePaperList.push(...data.records); // 将新获取的推文添加到现有列表
						this.currentPage++; // 更新页码
						this.hasMore = data.records.length === PageSize; // 如果当前页的数据量小于PageSize，表示没有更多数据
					} else {
						this.hasMore = false; // 如果没有更多数据，则标记没有更多数据
					}
				} catch (error) {
					console.error("Error fetching tweets:", error);
				} finally {
					this.loading = false; // 加载完成后，恢复loading状态
				}
			},
			// 滚动到底部时触发加载更多
			onScrollToLower() {
				this.getTweetsInfo(this.currentPage, this.PageSize);
			},
			async validToken() {
				const token = uni.getStorageSync("token");
				API.getTokenValid(token).then(res => {
					// console.log(typeof res.data);
					const data = res.data
					if (data != true) {
						uni.showToast({
							title: '登录过期,请重新登录',
							icon: 'none',
							duration: 3000
						});
						setTimeout(() => {
							uni.redirectTo({
								url: '/pages/login/login'
							});
						}, 2000)
					}
				})
			}
		},
		mounted() {
			this.validToken()
			this.getSwiperImg();
			this.getTweetsInfo(this.currentPage, this.PageSize);
		},
		created() {

		}
	}
</script>

<style>

</style>